<?php 
	include('function/connection.php');
	$pd_code = $pd_name = $buying_price = $selling_price = $pd_des = $date = $expire_date = "";
	$action ="insert";
	$btn = "Save";
	if($date =="" && $expire_date==""){
		$date=$expire_date=date("Y-m-d");
	}
	
	// Select customer info
	if(isset($_GET['id'])){
		$querySelectProduct = "SELECT * FROM tbl_products WHERE pd_id= $_GET[id]";
		$result = mysql_query($querySelectProduct);
		$row = mysql_fetch_array($result);
			$categoryId=$row['cat_id'];
			$pd_code = $row['pd_code'];
			$pd_name = $row['pd_name'];
			$buying_price = $row['buying_price'];
			$selling_price = $row['selling_price'];
			$pd_des = $row['pd_des'];
			$date = $row['date'];
			$expire_date = $row['expire_date'];
			
			$action = "update&id=".$_GET['id'];		
			$btn = "Update";
			
	}
?>

<form action="verify_products.php?action=<?php echo $action ?>&name=<?php echo $pd_name ?>" method="post" enctype="multipart/form-data" class="form" name="form1" id="form1">
	<table  width="770">
        <thead>
            <tr>
                <th>Please insert your products information:<br /><span class="red">* require feild</span></th>
            </tr>
        </thead>
        <tr>
    	<?php 
			if(@$errors!=""){
				echo " <td ><span class='red'>". @$errors."</span></td>";
			}else{
				if(isset($_REQUEST['msgStatus']))
					echo " <td ><span class='red'>". $_REQUEST['msgStatus']."</span></td>";
			 }
			?>
    </tr>
	</table>
    <table class="bordered" width="770">
    	<tr>
			<td>
            	<label><span class="red">*</span> Category:</label>
            </td> 
            <td>
            	<select name="cbocategory" class="input req-min" minlength="1" style="width:200px; height:30px;">
                    <option value=""> -- select catégorie -- </option>
						<?php
                           $sql = "SELECT * FROM tbl_category ORDER BY cat_id DESC";
                           $result = query($sql);
                           while($row = fetch_assoc($result))
                           {
                                        
                              if(@$categoryId == $row['cat_id'])
                                  $selected = " selected='selected'";
                              else	
                                  $selected = "";
                      ?>
                    <option value="<?php echo $row['cat_id'];?>"<?php echo $selected;?>>
                               <?php echo $row['cat_name'];?></option>
                     <?php
                          }           
                     ?>
                 </select>
            </td>
		</tr>
		<tr>
			<td>
            	<label><span class="red">*</span> Product Code:</label>
            </td> 
            <td><input type="text" class="input req-min" minlength="1" style="width:250px; height:25px;" name="txtPdCode" value="<?php echo $pd_code ?>" /></td>
		</tr>
        <tr>
        	<td>
            	<label><span class="red">*</span> Product Name:</label> 
            </td>
            <td><input type="text" class="input req-min" minlength="1" style="width:250px; height:25px;" name="txtPdName" value="<?php echo $pd_name ?>" /></td>
        </tr>
        <tr>
        	<td>
            	<label>Buying Price:</label>
           	</td>
            <td><input type="text" style="width:250px; height:25px;" name="txtBuying" value="<?php echo $buying_price ?>" /></td>
		</tr>
        <tr>
			<td>
            	<label>Selling Price:</label>
            </td>
            <td><input type="text" style="width:250px; height:25px;" name="txtSell" value="<?php echo $selling_price ?>" /></td>
		</tr>
        <tr>
        	 <td >
            	<label>Description:</label>
            </td>
            <td><input type="text" style="width:250px; height:25px;"  name="txtDes" value="<?php echo $pd_des ?>" /></td>
        </tr>
        <tr>
        	 <td >
            	<label>Date:</label>
            </td>
            <td><input type="text" style="width:250px; height:25px;" name="txtDate" id="txtDate" value="<?php echo $date ?>" /></td>
        </tr>
        <tr>
        	 <td >
            	<label>Expire Date:</label>
            </td>
            <td><input type="text" style="width:250px; height:25px;" name="txtExDate"  id="txtExDate" value="<?php echo $expire_date ?>" /></td>
        </tr>
		<tr>
        	<td></td>
			<td ><input type="submit" class="btnSubmit" id="Submit" name="Submit" value="<?php echo $btn ?>" />&nbsp; 
            <input type="button" class="btnSubmit" name="btnSubmit" value="Cancel" onclick="redirect('index.php?page=products&action=products')" /></td>
		</tr>
	</table>
</form>